package com.gly091020.enchantment;

import com.gly091020.*;
import com.gly091020.util.*;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentTarget;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundEvents;

import java.util.Objects;

import static com.gly091020.EnglishFight.ENGLISH_QUESTION;

public class EnglishFightEnchantment extends Enchantment {
    public static final EquipmentSlot[] slot = {EquipmentSlot.MAINHAND};
    private static final long COOLDOWN = 50;
    private static long lastDamageTime = 0;
    public EnglishFightEnchantment() {
        super(Rarity.RARE, EnchantmentTarget.WEAPON, slot);
    }

    @Override
    public int getMaxLevel() {
        return 1;
    }

    @Override
    public String getTranslationKey() {
        return "enchantment.english_fight.name";
    }

    @Override
    public boolean isAvailableForEnchantedBookOffer() {
        return true;
    }

    @Override
    public boolean isAvailableForRandomSelection() {
        return true;
    }

    @Override
    public void onTargetDamaged(LivingEntity user, Entity target, int level) {
        if(user.getWorld().isClient){return;}
        if(!(target instanceof LivingEntity)){return;}
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastDamageTime < COOLDOWN) {
            return;
        }
        lastDamageTime = currentTime;
        if(user.isPlayer()){
            if (user.getAttached(EnglishFight.ENGLISH_COMBO) == null){
                user.setAttached(EnglishFight.ENGLISH_COMBO, new EnglishCombo());
            }
            var combo = ((EnglishCombo) Objects.requireNonNull(user.getAttached(EnglishFight.ENGLISH_COMBO))).getCombo();
            if(user.getAttached(EnglishFight.ENGLISH_ANSWER) != null &&
                    user.getAttached(ENGLISH_QUESTION) != null){
                if(((EnglishAnswer) Objects.requireNonNull(user.getAttached(EnglishFight.ENGLISH_ANSWER))).getAnswer() ==
                        ((EnglishFightQuestion) Objects.requireNonNull(user.getAttached(ENGLISH_QUESTION))).getRightIndex()){
                    ((EnglishCombo) Objects.requireNonNull(user.getAttached(EnglishFight.ENGLISH_COMBO))).incrementCombo();
                    if (combo >= EnglishFight.config.canHurtCombo) {
                        target.damage(user.getWorld().getDamageSources().playerAttack((PlayerEntity) user),
                                ((float) (combo - EnglishFight.config.canHurtCombo) /
                                        (EnglishFight.config.maxCombo - EnglishFight.config.canHurtCombo)) * EnglishFight.config.damage);
                    }else{
                        if(((LivingEntity) target).getHealth() != 0){
                            ((LivingEntity)target).setHealth(((LivingEntity) target).getHealth() + Math.abs(combo - EnglishFight.config.canHurtCombo));
                        }
                    }
                }else{
                    ((EnglishCombo) Objects.requireNonNull(user.getAttached(EnglishFight.ENGLISH_COMBO))).resetCombo();
                }
                var q = EnglishFightFun.getRandomQuestion();
                user.setAttached(ENGLISH_QUESTION, q);
                ServerPlayNetworking.send((ServerPlayerEntity) user, NetworkID.UPDATE_QUESTION, EnglishFightFun.questionToBuffer(q));
                var p = PacketByteBufs.create();
                p.writeInt(((EnglishCombo) Objects.requireNonNull(user.getAttached(EnglishFight.ENGLISH_COMBO))).getCombo());
                ServerPlayNetworking.send((ServerPlayerEntity) user, NetworkID.UPDATE_COMBO, p);
            }
        }
    }
}
